package com.csdemos;

import com.csdemos.algs.Stack;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class StackDemoActivity extends Activity {
	
	private Stack stack;
	
	private EditText x;
	private TextView array;
	private TextView result;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_stack_demo);
		x = (EditText) findViewById(R.id.x);
		
		array = (TextView) findViewById(R.id.array);
		result = (TextView) findViewById(R.id.result);
		
		stack = new Stack();
		refresh();
	}
	
	private void setResultString(int result) {
		this.result.setText(String.valueOf(result));
	}
	
	private void refresh() {
		array.setText(stack.toString());
	}
	
	public void isEmpty(View view) {
		result.setText(String.valueOf(stack.isEmpty()));
	}
	
	public void pop(View view) {
		try {
			setResultString(stack.pop());
			refresh();
		} catch (Exception e) {
			result.setText(e.getMessage());
		}
		
	}
	
	public void push(View view) {
		stack.push(Integer.valueOf(x.getText().toString()));
		refresh();
	}

}
